﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace UI.NetworkDiagram
{
    public class Edge 
    {
        #region props
        public Vertice FromVertice { get; set; }
        public Vertice ToVertice { get; set; }
        public string Label { get; set; }
        public double Thickness { get; set; }
        public double Length { get; set; }
        public Color PenColor { get; set; }
        public EdgeLineStyle LineStyle { get; set; }
        public string Tooltip { get; set; }
        public bool Directional { get; set; }
        #endregion

        #region UI
        public Line Line { get; set; }
        public TextBlock TooltipBox { get; set; }
        #endregion

        #region ctor
        public Edge(Vertice fromVertice, Vertice toVertice,
            double distance, string tooltipText)
        {
            this.FromVertice = fromVertice;
            this.ToVertice = toVertice;
            this.Length = distance;
            this.Line = new Line();
            this.LineStyle = EdgeLineStyle.Solid;
            this.Label = distance.ToString();
            this.Thickness = 1.5;
            this.PenColor = Colors.Blue;
            this.Tooltip = tooltipText;
            this.Directional = false;
            this.Line.Stroke = new SolidColorBrush(this.PenColor);
            this.Line.StrokeThickness = this.Thickness;
            this.TooltipBox = new TextBlock();
            this.TooltipBox.Text = tooltipText;
            ToolTipService.SetToolTip(this.Line,this.TooltipBox);
        }
        #endregion

        #region draw
        public void SnapEdge()
        {
            Point p1 = this.FromVertice.Position.ToPoint();
            Point p2 = this.ToVertice.Position.ToPoint();
            double d = (this.FromVertice.Position - this.ToVertice.Position).Length;
            if (d > 0)
            {
                double dx = Math.Abs(p1.X - p2.X)/d*this.ToVertice.Ball.ActualWidth/2;
                double dy = Math.Abs(p1.Y - p2.Y)/d*this.ToVertice.Ball.ActualHeight/2;
                if (p1.X < p2.X)
                    p2.X -= dx;
                else
                    p2.X += dx;
                if (p1.Y < p2.Y)
                    p2.Y -= dy;
                else
                    p2.Y += dy;
            }
            if (this.LineStyle == EdgeLineStyle.Dashed)
            {
                this.Line.StrokeDashArray = new DoubleCollection();
                this.Line.StrokeDashArray.Add(1.0);
                this.Line.StrokeDashArray.Add(1.0);
                this.Line.Stroke = new SolidColorBrush(this.PenColor);
                this.Line.StrokeThickness = this.Thickness;
            }

            this.Line.X1 = p1.X;
            this.Line.Y1 = p1.Y;
            this.Line.X2 = p2.X;
            this.Line.Y2 = p2.Y;

            if (this.Directional)
                this.Line.StrokeEndLineCap = PenLineCap.Triangle;
            else
                this.Line.StrokeEndLineCap = PenLineCap.Round;
        }
        #endregion
    }
}
